<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 1000px;
            height: 500px;
            border: 1px solid black;
            margin: 20px auto;
            position: relative;
        }
        
        img {
            width: 300px;
            position: absolute;
        }
    </style>
</head>

<body>
    <div class="box">
        <img src="http://contentcms-bj.cdn.bcebos.com/cmspic/a3e3de575e8807c1063fc6d476abee1e.jpeg">
    </div>
</body>

</html>
<script>
    //限定范围的拖拽
    //图片位置不再以可视区为基准，以box为基准，但是鼠标位置以可视区为基准。
    var oImg = document.querySelector('img');
    var box = document.querySelector('.box');
    oImg.onmousedown = function(ev) {
        var ev = ev || event;
        var shortlinex = ev.offsetX;
        var shortliney = ev.offsetY;
        document.onmousemove = function(ev) {
            var ev = ev || event;
            //为什么减去box的位置，盒子以box为基准进行位置的改变。
            var left = ev.clientX - shortlinex - box.offsetLeft; //盒子的left位置
            var top = ev.clientY - shortliney - box.offsetTop; //盒子的left位置

            if (left <= 0) {
                left = 0;
            } else if (left >= box.offsetWidth - oImg.offsetWidth) {
                left = box.offsetWidth - oImg.offsetWidth;
            }

            if (top <= 0) {
                top = 0;
            } else if (top >= box.offsetHeight - oImg.offsetHeight) {
                top = box.offsetHeight - oImg.offsetHeight;
            }
            oImg.style.left = left + 'px';
            oImg.style.top = top + 'px';
        };

        document.onmouseup = function() {
            document.onmousemove = null;
            document.onmouseup = null;
        };

        return false; //取消图片的默认行为。
    }


    // oImg.onmousedown = function(ev) {
    //     var ev = ev || event;
    //     var shortlinex = ev.offsetX;
    //     var shortliney = ev.offsetY;
    //     document.onmousemove = function(ev) {
    //         var ev = ev || event;
    //         //限定范围
    //         var left = ev.clientX - shortlinex; //盒子的left位置
    //         var top = ev.clientY - shortliney; //盒子的left位置
    //         if (left < 0) {
    //             left = 0;
    //         } else if (left > document.documentElement.clientWidth - oImg.offsetWidth) {
    //             left = document.documentElement.clientWidth - oImg.offsetWidth;
    //         }

    //         if (top < 0) {
    //             top = 0;
    //         } else if (top > document.documentElement.clientHeight - oImg.offsetHeight) {
    //             top = document.documentElement.clientHeight - oImg.offsetHeight;
    //         }
    //         oImg.style.left = left + 'px';
    //         oImg.style.top = top + 'px';
    //     };
    //     document.onmouseup = function() {
    //         document.onmousemove = null;
    //         document.onmouseup = null;
    //     };

    //     return false; //取消图片的默认行为。
    // }
</script>